<template>
    <div>
        <el-button @click="showSaveDialog=true" size="mini" class="long">导出</el-button>
        <el-dialog
                title="保存筛选结果"
                :visible.sync="showSaveDialog"
                :append-to-body="true"
                center>
            <el-input placeholder="文件名" v-model="filename" @keyup.enter.native="exportFile"></el-input>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exportFile" class="long">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import XLSX from 'xlsx';
    import {mapGetters} from 'vuex'

    export default {
        name: "SaveResult",
        data() {
            return {
                showSaveDialog: false,
                filename: '',
            }
        },
        computed: {
            ...mapGetters(['results']),
        },
        methods: {
            exportFile() {
                let row = this.results.map(r => {
                    let ret = Object.assign({}, r);
                    delete ret.id;
                    return ret;
                });
                if (this.filename.length <= 0) {
                    this.filename = 'res_' + new Date().getTime();
                }
                // eslint-disable-next-line no-console
                // console.log(rows);
                let wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'};//这里的数据是用来定义导出的格式类型

                const wb = {SheetNames: ['Sheet1'], Sheets: {}, Props: {}};
                let data = XLSX.utils.json_to_sheet(row);//通过json_to_sheet转成单页(Sheet)数据
                // data['!merges'] = merges;
                wb.Sheets['Sheet1'] = data;
                this.saveAs(new Blob([this.s2ab(XLSX.write(wb, wopts))], {type: "application/octet-stream"}),
                    this.filename + '.' + (wopts.bookType === "biff2" ? "xls" : wopts.bookType));
                this.showSaveDialog = false;
            },
            saveAs(obj, fileName) {//当然可以自定义简单的下载文件实现方式
                var tmpa = document.createElement("a");
                tmpa.download = fileName || "下载";
                tmpa.href = URL.createObjectURL(obj); //绑定a标签
                tmpa.click(); //模拟点击实现下载
                setTimeout(function () { //延时释放
                    URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
                }, 100);
            },
            s2ab(s) {
                let buf;
                let index;
                if (typeof ArrayBuffer !== 'undefined') {
                    buf = new ArrayBuffer(s.length);
                    var view = new Uint8Array(buf);
                    for (index = 0; index <= s.length; ++index) view[index] = s.charCodeAt(index) & 0xFF;
                    return buf;
                } else {
                    buf = new Array(s.length);
                    for (index = 0; index <= s.length; ++index) buf[index] = s.charCodeAt(index) & 0xFF;
                    return buf;
                }
            }
        }
    }
</script>

<style scoped>

</style>